package com.lyh.ai.controller;

import jakarta.activation.MimeType;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;
import org.stringtemplate.v4.ST;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ChatController {

    private final ZhiPuAiChatModel zhiPuAiChatModel;

    @Autowired
     public ChatController(ZhiPuAiChatModel zhiPuAiChatModel) {
        this.zhiPuAiChatModel = zhiPuAiChatModel;
    }


    @GetMapping("/chat1")
    public String chat1(@RequestParam("message") String message) {
        return zhiPuAiChatModel.call(message);
    }

    @GetMapping("/chat2")
    public String chat2(@RequestParam("message") String message) {
        // 默认模型为 glm-4-air，可以在ZhiPuAiChatOptions类中的getModel方法debug查看
        ChatResponse chatResponse = zhiPuAiChatModel.call(
                new Prompt(message,
                        ZhiPuAiChatOptions.builder()
                                .withModel(ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue())
                                .withTemperature(0.7f)
                                .build()
                )
        );
        return chatResponse.getResult().getOutput().getContent();
    }


    @GetMapping("/chat3")
    public String chat3(@RequestParam("message") String message) {
        // 默认模型为 glm-4-air，可以在ZhiPuAiChatOptions类中的getModel方法debug查看
        Flux<ChatResponse> stream = zhiPuAiChatModel.stream(
                new Prompt(message,
                        ZhiPuAiChatOptions.builder()
                                .withModel(ZhiPuAiApi.ChatModel.GLM_3_Turbo.getValue())
                                .withTemperature(0.7f)
                                .build()
                )
        );
        StringBuilder sb = new StringBuilder();
        stream.toStream().forEach(item -> {
            sb.append(item.getResult().getOutput().getContent());
        });
        return sb.toString();
    }


}
